<?php
namespace app\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;

class WeixincallbackController extends ActiveController
{
	
    public $modelClass = 'app\modules\v1\models\MerchantWeixin';
    
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['update'], $actions['create'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionIndex()
    {
    	// 依据code码去获取openid和access_token，自己的后台服务器直接向微信服务器申请即可
    	$appid 		= Yii::$app->params['appid'];
    	$appsecret 	= Yii::$app->params['appsecret'];
    	if (isset($_GET['code'])){
    		$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$_GET['code']."&grant_type=authorization_code";
    		$data = null;
	    	$curl = curl_init();
	    	curl_setopt($curl, CURLOPT_URL, $url);
	    	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	    	if (!empty($data)){
	    		curl_setopt($curl, CURLOPT_POST, 1);
	    		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	    	}
	    	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    	$output = curl_exec($curl);
	    	curl_close($curl);
	    	$res=(json_decode($output, true));
	    	$redi_status    = $_GET['state'];   //1跳转到我的收款；2跳转到我要注册
	    	if($redi_status == 1){
	    		$url = 'http://wx.78dk.com/weixin/src/index.html?openid='.$res['openid'];
	    		echo "<script>location='".$url."'</script>";
	    		//header("Location:$url"); 
	    		exit;
	    	}elseif ($redi_status == 2){
	    		$url = 'http://wx.78dk.com/weixin/src/views/market_info.html?openid='.$res['openid'];
	    		echo "<script>location='".$url."'</script>";
	    		//Header("Location:$url");
	    		exit;
	    	}
    	}else{
    		echo 'code为空';
    	}
    	
    	exit;
    	
    }

}